package app.netant.finishbook.dztxt;

import org.jsoup.select.Elements;

import app.netant.NetAntBase;

public class DztxtDetail extends NetAntBase {
	
	private DztxtEntity entity;
	
	private String exp = "";

	public DztxtDetail(String netSite) {
		super(netSite);
	}
	
	public void setEntity(DztxtEntity entity){
		this.entity = entity;
	}
	
	public void analyseTitle(){
		Elements activePart = document.select("body .section .book_info h1");
		if(activePart.size()>0){
			String title = activePart.get(0).text().trim();
			System.out.println("【Title】:"+title);
			this.entity.setNovelName(title);
			this.entity.setNovelFullName(title+".txt");
		}
	}
	
	public void analyseTags(){
		Elements activePart = document.select("body .section .book_info .infos span");
		for(int i=0;i<activePart.size();i++){
			String text = activePart.get(i).text().trim();
			int idx = text.indexOf("：");
			if(idx>0){
				String tagName = text.substring(0,idx).trim();
				String tagValue = text.substring(idx+1).trim();
				System.out.println("【Tag】:"+tagName+"="+tagValue);
				this.entity.setTag(tagName, tagValue);
				if("下载方式".equals(tagName)){
					String href = activePart.get(i).select("a").get(0).attr("href");
					System.out.println("【Href】:"+href);
					this.entity.setDownPath(href);
				}
			}
			
		}
	}
	
	public void analyseMemo(){
		Elements activePart = document.select("body .section .book_info p");
		if(activePart.size()>0){
			String memo = activePart.get(0).text().trim();
			System.out.println("【Memo】:"+memo);
			this.entity.setNovelMemo(memo);
		}
	}
	
	public void analyse(){
		
		this.analyseTitle();
		this.analyseTags();
		this.analyseMemo();
		
		this.entity.download();
		this.entity.insert();
		
	}

	public static void main(String[] args) {
		
		DztxtDetail detail = new DztxtDetail("https://www.dztxt.com/book_11777/");
		
		detail.analyseTitle();
		detail.analyseTags();
		detail.analyseMemo();
	}
	
}
